<?php

require_once "Connect.php";

class Customer extends Connect {

    function saveCustomer($arrData){
        $arrDefault = array("customer_name"=>null, "address" =>null, "phone"=>null, "information" => null );
        $arrData = array_merge($arrDefault,$arrData);
        try{            
            $sql = "INSERT INTO customer VALUES(NULL,'".$arrData['customer_name']."','".$arrData['address']."','".$arrData['phone']."','".$arrData['email']."','".$arrData['information']. "',1,UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW())) ";
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: saveCustomer()");
            }else{
                $customer_id = mysql_insert_id();
                mysql_query("INSERT INTO report_customer VALUES(".$customer_id.",0,0,UNIX_TIMESTAMP(NOW()))");
                return $customer_id;
            }            
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function editCustomer($arrData){
        $arrDefault = array("customer_name"=>null, "address" =>null, "phone"=>null, "information" => null );
        $arrData = array_merge($arrDefault,$arrData);
        try{      
            $sql = "UPDATE customer SET customer_name = '".$arrData['customer_name']."',address='".$arrData['address']."',phone='".$arrData['phone']."',email='".$arrData['email']."',information = '".$arrData['information']. "',update_time=UNIX_TIMESTAMP(NOW()) WHERE customer_id = ".$arrData['customer_id'];            
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: editCustomer()");
            }else{               
                return $rs;
            }            
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function saveOrder($arrData){  
        $arrDefault = array("description"=>null, "date_giao" =>null, "date_tra"=>null, "total_price" => null );
        $arrData = array_merge($arrDefault,$arrData);
        try{
            $sql = "INSERT INTO orders VALUES(NULL,".$arrData['customer_id'].",'".$arrData['order_no']."','".$arrData['total_product']."','".$arrData['total_price'];
            $sql.= "',".$arrData['method_id'].",".$arrData['payment_id'].",'".$arrData['date_giao'];
            $sql.="','".$arrData['date_tra']."','".$arrData['description']."',".$arrData['order_status'];
            $sql.=",1,UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW())) ";
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: saveOrder()");
            }else{
                $order_id = mysql_insert_id();
                $sql2 = "UPDATE report_customer SET total_order = (total_order + 1), total_product = total_product + ".$arrData['total_product'].",update_time = UNIX_TIMESTAMP(NOW()) WHERE customer_id =  ".$arrData['customer_id'];
                mysql_query($sql2);
                return $order_id;
            }            
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function editOrder($arrData){  
        $arrDefault = array("description"=>null, "date_giao" =>null, "date_tra"=>null, "total_price" => null );
        $arrData = array_merge($arrDefault,$arrData);
        try{
            $sql = "UPDATE orders SET order_no = '".$arrData['order_no']."',total_product='".$arrData['total_product']."',total_price='".$arrData['total_price']."',method_id='".$arrData['method_id']."',payment_id = '".$arrData['payment_id']."',date_giao='".$arrData['date_giao']."',date_tra='".$arrData['date_tra']."',description='".$arrData['description']. "',order_status=".$arrData['order_status'].",update_time=UNIX_TIMESTAMP(NOW()) WHERE order_id = ".$arrData['order_id'];                        
           
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: editOrder()");
            }else{              
                return $rs;
            }            
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function saveOrderDetail($arrData){  
        $arrDefault = array("url_images"=>null);
        $arrData = array_merge($arrDefault,$arrData);
        try{           
            $sql = "INSERT INTO order_detail VALUES(NULL,'".$arrData['order_id']."','".$arrData['product_code']."','".$arrData['number_product'];
            $sql.= "',".$arrData['price'].",".$arrData['total_price'].",'".$arrData['mau_id'];
            $sql.="','".$arrData['size_id']."','".$arrData['kieu_id']."','".$arrData['url_images'];
            $sql.="',1,UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW())) ";
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: saveOrderDetail()");
            }else{
                $order_detail_id = mysql_insert_id();
                $rs1 = mysql_query("SELECT * FROM order_detail WHERE order_id = ".$arrData['order_id']." AND status = 1");
                $totalPrice = $totalProduct = 0 ;
                while($row1 = mysql_fetch_assoc($rs1)){
                    $totalPrice += $row1['total_price'];
                    $totalProduct += $row1['number_product'];
                }
                mysql_query("UPDATE orders SET total_product = $totalProduct,total_price = $totalPrice WHERE order_id = ".$arrData['order_id']);
                return $order_detail_id;
            }            
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function saveSoDo($arrData){                
        try{            
            mysql_query("DELETE FROM sodo WHERE customer_id = ".$arrData['customer_id']);
            $sql = "INSERT INTO sodo VALUES(".$arrData['customer_id'].",".$arrData['vong_nguc'].",".$arrData['vong_eo'].",".$arrData['vong_mong'].",
                                                ".$arrData['ha_nguc'].",".$arrData['ha_eo'].",".$arrData['rong_vai'].",".$arrData['dai_tay_ao'].",
                                                ".$arrData['dai_ao'].",".$arrData['vong_co'].",".$arrData['vong_nach'].",".$arrData['bap_tay_tren'].",
                                                ".$arrData['bap_tay_duoi'].",".$arrData['vong_day'].",".$arrData['vong_nguc_tren'].",".$arrData['vong_nguc_duoi'].",
                                                ".$arrData['dai_quan']. ",UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW()),1) ";
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: saveSoDo()");
            }else{              
                return $rs;
            }            
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function getListMethod() {
        try{
            $sql = "SELECT method_id, method_name FROM method WHERE status = 1";
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: getListMethod()");
            }
            return $rs;
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function getListPayment() {
        try{
            $sql = "SELECT payment_id, payment_name FROM payment WHERE status = 1";
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: getListPayment()");
            }
            return $rs;
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function getListCustomer($phone = '' , $tukhoa = '', $limit = -1, $offset = -1) {
        try{
        $sql = "SELECT * FROM customer WHERE status = 1 ";
        if ($tukhoa != "")
            $sql.=" AND customer_name LIKE '%$tukhoa%' ";
        if ($phone != "")
            $sql.=" AND phone LIKE '%$phone%' ";
        $sql.="	ORDER BY customer_id DESC ";
        if ($limit > 0 && $offset >= 0)
            $sql.= " LIMIT $offset,$limit";

        $rs = mysql_query($sql) or die(mysql_error());
        if($rs==false){
            throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: getListCustomer()");
        }
        return $rs;
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }    
    function getListOrderByCustomer($customer_id=-1,$order_no='',$order_status=-1,$fromdate='',$todate='',$offset=-1,$limit=-1) {
        try{ 
        $sql = "SELECT orders.*,customer_name,phone,payment_name,method_name FROM orders,customer,payment,method
                    WHERE orders.customer_id = customer.customer_id AND  (orders.customer_id = $customer_id OR $customer_id = -1 ) AND  (order_status = $order_status OR $order_status = -1 )
                    AND payment.payment_id = orders.payment_id AND orders.method_id = method.method_id AND orders.status = 1 " ;
        if($order_no != '') $sql.=" order_no LIKE '%$order_no%' ";
        if($fromdate != '' && $todate==''){
            $fromdate = strtotime($fromdate);
            $sql.=" AND date_giao >= $fromdate";
        }
        if($fromdate == '' && $todate!=''){
            $todate = strtotime($todate);
            $sql.=" AND date_giao <= $todate";
        }
        if($fromdate != '' && $todate!=''){
            $fromdate = strtotime($fromdate);
            $todate = strtotime($todate);
            $sql.=" AND ( date_giao <= $todate && date_giao >= $fromdate )";
        }
        $sql.= " ORDER BY date_giao ASC ";   
        if ($limit > 0 && $offset >= 0)
            $sql.= " LIMIT $offset,$limit";      
        $rs = mysql_query($sql) or die(mysql_error());
        if($rs==false){
            throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: getListOrderByCustomer()");
        }
        return $rs;
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }  
    function getDetailCustomer($customer_id){
        $rs = mysql_query("SELECT * FROM customer WHERE customer_id = $customer_id");
        $row = mysql_fetch_assoc($rs);
        return $row;
    }
    function getListKieu() {
        try{
            $sql = "SELECT kieu_id, kieu_name FROM kieu";
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: getListKieu()");
            }
            return $rs;
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function getListSize() {
        try{
            $sql = "SELECT size_id, size_name FROM size";
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: getListSize()");
            }
            return $rs;
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function getListMau() {
        try{
            $sql = "SELECT mau_id, mau_name FROM mau";
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: getListMau()");
            }
            return $rs;
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function getListChat() {
        try{
            $sql = "SELECT chat_id, chat_name FROM chatlieu";
            $rs = mysql_query($sql);
            if($rs==false){
                throw new Exception(mysql_error()."- SQL :: ".$sql."- Function :: getListChat()");
            }
            return $rs;
        } catch (Exception $e) {            
            echo 'Có lỗi xảy ra: ',  $e->getMessage(), "\n";
        }
    }
    function getDetailOrder($order_id){
        $rs = mysql_query("SELECT * FROM orders WHERE order_id = $order_id");
        $row = mysql_fetch_assoc($rs);
        return $row;
    }
    function phantrang($page,$page_show,$total_page,$link){
		$dau=1;
		$cuoi=0;
		$dau=$page - floor($page_show/2);		
		if($dau<1) $dau=1;	
		$cuoi=$dau+$page_show;
		if($cuoi>$total_page)
		{
			
			$cuoi=$total_page+1;
			$dau=$cuoi-$page_show;
			if($dau<1) $dau=1;
		}
		echo "<div id='thanhphantrang'>";
		if($page > 1){
			($page==1) ? $class = " class='selected'" : $class="" ;	
			echo "<a".$class." href=".$link."&page=1>Đầu</a>"	;							
		}
		for($i=$dau; $i<$cuoi; $i++)
		{
			($page==$i) ? $class = " class='selected'" : $class="" ;		
			echo "<a".$class." href=".$link."&page=$i>$i</a>";			
		}
		if($page < $total_page) { 
			($page==$total_page) ? $class = " class='selected'" : $class="" ;		
			echo "<a".$class." href=".$link."&page=$total_page>Cuối</a>";
		}
		echo "</div>";
	}
    
}

?>